/*
 * @Author: xiaosihan
 * @Date: 2023-02-06 15:22:57
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-10-01 19:23:41
 */
import utils from "@utils";
import {
  AmbientLight,
  DirectionalLight,
  EquirectangularReflectionMapping,
  HemisphereLight,
  LinearSRGBColorSpace,
  MeshPhysicalMaterial,
  SRGBColorSpace,
  Vector3,
} from "three";
import ThreeBase from "three-base";
import PointLight from "three-base/PointLight/PointLight";
import threeLoader from "three-base/threeLoader";
import threeUtils from "three-base/threeUtils";
import BlastMesh from "./component3d/BlastMesh/BlastMesh";
import blueskyJPG from "./img/bluesky.jpg";

/**
 * @class HomeRender
 * @extends {ThreeBase}
 */
class HomeRender extends ThreeBase {
  constructor() {
    super();
  }

  // 环境光
  ambientLight = (() => {
    const ambientLight = new AmbientLight("#fff", 3);
    this.scene.add(ambientLight);
    return ambientLight;
  })();

  //点光源
  pointLight = (() => {
    const pointLight = new PointLight("#fff", 1);
    pointLight.distance = 1000;
    pointLight.decay = 0;
    pointLight.position.set(50, 40, 50);
    this.scene.add(pointLight);
    return pointLight;
  })();

  //半球光
  hemisphereLight = (() => {
    const hemisphereLight = new HemisphereLight("#c7f8ff", "#000000", 0.5);
    // this.scene.add(hemisphereLight);
    return hemisphereLight;
  })();

  // 方向光2
  directionalLight2 = (() => {
    const directionalLight = new DirectionalLight("#fff", 0.5);
    directionalLight.position.set(10, 10, 0);
    directionalLight.target.position.set(0, 0, 0);
    directionalLight.castShadow = true;
    directionalLight.shadow.mapSize.set(1024, 1024);
    // this.scene.add(directionalLight);
    return directionalLight;
  })();

  // 方向光
  directionalLight = (() => {
    const directionalLight = new DirectionalLight("#fff", 0.5);
    directionalLight.position.set(10, 10, 5);
    directionalLight.target.position.set(0, 0, 0);
    directionalLight.castShadow = true;
    directionalLight.shadow.mapSize.set(1024, 1024);
    // this.scene.add(directionalLight);
    return directionalLight;
  })();

  init() {
    super.init();
    this.axesHelper.visible = false;
    this.gridHelper.visible = false;
    this.controls.enableZoom = false;
    this.controls.enablePan = false;
    this.controls.enableDamping = false;
    this.autoRender = utils.isDev;

    const envMap = threeLoader.getTexture(blueskyJPG).clone();
    envMap.mapping = EquirectangularReflectionMapping;
    this.scene.environment = envMap;
    this.scene.environmentIntensity = 0.4;

    this.resetLookAt({
      center: new Vector3(0.07, -0.294, 0.045),
      position: new Vector3(4.349, 3.131, 5.951),
    });
  }

  //爆炸模型
  blastMesh = (() => {
    const blastMesh = new BlastMesh();
    blastMesh.loadModel("./homeModal2.glb");
    blastMesh.addEventListener("loaded", async () => {
      threeUtils.scaleTo(blastMesh, 5);
      this.scene.add(blastMesh);
      blastMesh.traverseMesh((mesh) => {
        if (["Obj3d66-12887158-10-749"].includes(mesh.name)) {
          (mesh.material as MeshPhysicalMaterial).color.set("#f1fbff");
          (mesh.material as MeshPhysicalMaterial).metalness = 0.79;
          (mesh.material as MeshPhysicalMaterial).roughness = 0.56;
          (mesh.material as MeshPhysicalMaterial).envMapIntensity = 0.4;
        } else if (["Obj3d66-12887158-20-435"].includes(mesh.name)) {
          (mesh.material as MeshPhysicalMaterial).color.set("#4f4f4f");
          (mesh.material as MeshPhysicalMaterial).metalness = 1;
          (mesh.material as MeshPhysicalMaterial).roughness = 0.49;
          (mesh.material as MeshPhysicalMaterial).envMapIntensity = 0.1;
        } else if (["Obj3d66-12887158-13-943"].includes(mesh.name)) {
          (mesh.material as MeshPhysicalMaterial).color.set("#bfbfbf");
          (mesh.material as MeshPhysicalMaterial).metalness = 1;
          (mesh.material as MeshPhysicalMaterial).roughness = 0.36;
          (mesh.material as MeshPhysicalMaterial).envMapIntensity = 0.51;
        }

        //three事件
        mesh.userData = {
          cursor: "default",
          enableEvent: !utils.isProd,
        };
        mesh.addEventListener("dbclick", (e) => {
          console.log(mesh.name);
          utils.editorMaterial(
            mesh.material as MeshPhysicalMaterial,
            mesh.name,
            () => {
              this.needRender = true;
            }
          );
        });
      });
    });
    return blastMesh;
  })();

  render() {
    this.pointLight.position
      .copy(this.getCamera().position)
      .multiply(new Vector3(1, 1, -1));

    super.render();
  }
}

const homeRenderer = (window.homeRenderer = new HomeRender());

export default homeRenderer;
